package cn.szabadsag.org.controller;


import cn.szabadsag.org.domain.Shop;
import cn.szabadsag.org.domain.vo.ShopRejectVo;
import cn.szabadsag.org.query.EmployeeQuery;
import cn.szabadsag.org.service.IShopService;
import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商铺接口
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "部门维护的接口")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 驳回商铺入驻申请
     * @param shopRejectVo  驳回商铺信息
     * @return
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopRejectVo shopRejectVo){
        try {
            System.out.println("_controller驳回商铺信息:" + shopRejectVo);
            shopService.reject(shopRejectVo);
            return AjaxResult.me();
        } catch (Exception e) //业务异常捕获
        {
            return AjaxResult.me().setMessage("驳回商铺入驻申请失败!"+e.getMessage());
        }
    }

    /**
     * 审核通过 approved
     */
    @PutMapping("/approved")
    public AjaxResult passShopReg(@RequestBody Map<String,Long> map){
        try {
            //  去业务层修改激活状态
            System.out.println(map.get("shop_Id"));

            shopService.passShopReg(map.get("shop_Id"));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    /**
     * 商铺入驻
     * @param shop  商铺入驻信息
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.settledIn(shop);
            return AjaxResult.me();
        /*} catch (BusinessException e) //业务异常捕获*/
        } catch (Exception e) //业务异常捕获
        {
            return AjaxResult.me().setMessage("入驻失败!"+e.getMessage());
        }
        /*catch (Exception e) { //500
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!"+e.getMessage());
        }*/
    }


    /**
     * 新增或修改
     */
    @PutMapping
    public AjaxResult saveOrUpt(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                //  新增
                shopService.save(shop);
            }else {
                //  修改
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 查询单个
     */
    @GetMapping(value = "/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询部门的方法")
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }

    /**
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public QueryResult<Shop> queryPage(@RequestBody EmployeeQuery query){
        return  shopService.queryPage(query);
    }

}
